package com.project.Dao.impl;

import com.project.Dao.ILinkDao;
import com.project.bean.CutPageBean;
import com.project.bean.LinkBean;
import org.springframework.stereotype.Repository;
import java.sql.SQLException;
import java.time.LocalDate;

/**
 * @Title: LinkDaoImpl
 * @Package: com.project.Dao
 * @Description:
 * @Author: Liang-Xie
 * @Date: 2022/6/22 - 17:12
 */
@SuppressWarnings("all")
@Repository
public class LinkDaoImpl extends BaseDao implements ILinkDao {

    @Override
    public CutPageBean<LinkBean> cutAll(int pageNum) {
        CutPageBean<LinkBean> cutBean = new CutPageBean<>();
        getConnection();
        try {
            ps = con.prepareStatement("select * from t_link limit ? , ? ");
            ps.setObject(1, (pageNum - 1) * CutPageBean.PAGESIZE);
            ps.setObject(2, CutPageBean.PAGESIZE);
            rs = ps.executeQuery();
            while (rs.next()) {
                LinkBean link = this.getBean();
                cutBean.getList().add(link);
            }

            ps = con.prepareStatement("select count(*) c from t_link");
            rs = ps.executeQuery();
            if (rs.next()) {
                cutBean.setCount(rs.getInt("c"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            this.closeConnection();
        }
        return cutBean;
    }

    @Override
    public LinkBean findById(int id) {
        LinkBean link = null;
        getConnection();
        try {
            ps = con.prepareStatement("select * from t_link where pk_linkId=? ");
            ps.setObject(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                link = getBean();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeConnection();
        }
        return link;
    }

    @Override
    public void add(LinkBean linkBean) {
        getConnection();
        try {
            ps = con.prepareStatement("insert into t_link(l_name,l_phone,l_birthday)values (?,?,?);");
            ps.setObject(1, linkBean.getName());
            ps.setObject(2, linkBean.getPhone());
            ps.setObject(3, linkBean.getBirthday());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeConnection();
        }


    }

    @Override
    public void update(int id, String phone) {
        getConnection();
        try {
            ps = con.prepareStatement("update t_link set l_phone=? where pk_linkId=? ");
            ps.setObject(1, phone);
            ps.setObject(2, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeConnection();
        }
    }

    @Override
    public void del(Integer id) {
        getConnection();
        try {
            ps = con.prepareStatement("delete from t_link where pk_linkId=? ");
            ps.setObject(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeConnection();
        }
    }

    public LinkBean getBean() throws SQLException {
        LinkBean linkBean = new LinkBean();
        linkBean.setId(rs.getInt("pk_linkId"));
        linkBean.setName(rs.getString("l_name"));
        linkBean.setPhone(rs.getString("l_phone"));
        if (rs.getString("l_birthday")==null){
            linkBean.setBirthday(null);
        }else {
            linkBean.setBirthday(LocalDate.parse(rs.getString("l_birthday")));
        }
        return linkBean;
    }
}
